import os

# 定义读取persons文件函数
def read_movies_data(path):

    # 检查文件是否存在，如果不存在则打印消息
    if not os.path.exists(path):
        print("文件不存在")
    else:
        # 如果文件存在则打开，用读模式 并命别名为f
        with open(path,"r") as f:

            # f是读取的数据，for循环进行遍历
            for line in f:

                # line 是persons文件中的一行数据，需要进行分割
                print(" line : " , line)
                movies_data = line.split(",")
                print(movies_data)



class Movie:# 定义电影类
    def __init__(self,no,mingcheng,zongpiaofang,pingfen):
        self.no = no
        self.mingcheng = mingcheng
        self.zongpiaofang = zongpiaofang
        self.pingfen = pingfen



class MovieMS:# 定义电影管理类
    movies = []
    def __init__(self):
        pass
    def menu(self):
        print("1-读取文件并清洗数据及保存到列表(输入数字1)")
        print("2-显示前后10条信息(输入数字2)")
        print("3-统计(输入数字3)")
        print("4-新增电影并保存到文件(输入数字4)")

        function_num = int(input("请输入数字选择功能"))

        if function_num == 1:
            print("欢迎使用读取文件并清洗数据及保存到列表(输入数字1)")
            self.do_read()
        elif function_num == 2:
            print("欢迎使用显示前后10条信息(输入数字2)")
            self.do_pb10_display()
        elif function_num == 3:
            print("欢迎使用统计(输入数字3)")
            self.do_statistic()
        elif function_num == 4:
            print("欢迎使用新增电影并保存到文件(输入数字4)")
            self.do_add_save()
        else:
            print("请输入正确的数字")




    def do_read(self):#采用文件数据（读）功能1
        path = "data/movies_data.txt"
        if not os.path.exists(path):
            print("文件不存在")
        else:
            with open(path,"r") as f:
                index = 0
                for line in f:
                    if index == 0:
                        index += 1
                        continue
                    else:
                        print(line)
                        movie_data = line.split(";")
                        one_movie = Movie(movie_data[0],movie_data[1],movie_data[2],movie_data[3])
                        self.movies.append(one_movie)
                        index +=1




    def do_pb10_display(self):#陈列电影数据元素    功能2
        for i in self.movies:
            print(i.no,i.mingcheng,i.zongpiaofang,i.pingfen)



    def do_statistic(self):#功能3
        f = open("data/movies_data.txt", "r")
        pingjunpingfen = 0
        zongpingfen = 0
        index = 0
        for line, i in f.readlines():
            if i == 1:
                continue
            else:
                index = index + 1
                lines = list(line)
                zongpingfen += lines[2]
        pingjunpingfen = zongpingfen / index
        print("总评分：", zongpingfen)
        print("平均评分：", pingjunpingfen)
        print("电影数", index)


    def do_add_save(self):#功能4
        no = input("请输入序号")
        mingcheng = input("请输入电影名称")
        piaofang = input("请输入电影票房")
        pingfen = input("请输入电影评分")

        movie = Movie(no, mingcheng, piaofang, pingfen)
        self.movies.append(movie)

        line = no + ";" + mingcheng + ";" + piaofang + ";" + pingfen + "\n"
        with open("data/movies_data.txt", "a") as f:
            f.write(line)



if __name__ == '__main__':

    a = MovieMS()
    while 1:
        a.menu()
